package com.zscat.mallplus.controller.single;

import com.zscat.mallplus.entity.dto.ApplyAftersalesDto;
import com.zscat.mallplus.entity.vo.oms.AftersalesDetailsVo;
import com.zscat.mallplus.entity.vo.oms.AftersalesInfoVo;
import com.zscat.mallplus.entity.vo.oms.AftersalesVo;
import com.zscat.mallplus.entity.vo.oms.ApplyAftersalesInfoVo;
import com.zscat.mallplus.service.oms.service.IOmsAftersalesService;
import com.zscat.mallplus.service.ums.service.IUmsMemberService;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.vo.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @Author ZuRongTang
 * @Date 2020/3/5
 **/

@RestController
@Api("售后服务")
@RequestMapping("/api/ftersales")
public class AftersalesController {


    @Resource
    private IUmsMemberService memberService;

    @Resource
    private IOmsAftersalesService iOmsAftersalesService;


    //申请
    @ApiOperation("获取申请售后列表")
    @RequestMapping(value = "/getApplyAftersalesList", method = RequestMethod.GET)
    public Object getApplyAftersalesList(Integer pageIndex, Integer pageSize) {
        CommonResult commonResult = new CommonResult();

        Long memberId = memberService.getNewCurrentMember().getId();

        if (memberId == null) {
            return commonResult.failed("请登录");
        }

        if (pageIndex == null) {
            return commonResult.paramFailed();
        }

        if (pageSize == null) {
            return commonResult.paramFailed();
        }

        PageInfo<AftersalesVo> applyAftersalesList = iOmsAftersalesService.getApplyAftersalesList(memberId, pageIndex, pageSize);

        if (applyAftersalesList == null) {
            return commonResult.success(new ArrayList<>());
        }

        return commonResult.success(applyAftersalesList);

    }


    //售后
    @ApiOperation("获取售后列表")
    @RequestMapping(value = "/getAftersalesList", method = RequestMethod.GET)
    public Object getAftersalesList(Integer pageIndex, Integer pageSize) {
        CommonResult commonResult = new CommonResult();

        Long memberId = memberService.getNewCurrentMember().getId();

        if (memberId == null) {
            return commonResult.failedIsNull("请登录");
        }

        if (pageIndex == null) {
            return commonResult.paramFailed();
        }

        if (pageSize == null) {
            return commonResult.paramFailed();
        }
        PageInfo<AftersalesInfoVo> aftersalesList = iOmsAftersalesService.getAftersalesList(memberId, pageIndex, pageSize);

        return commonResult.success(aftersalesList);
    }

    @ApiOperation("获取售后详情")
    @RequestMapping(value = "/getAftersalesInfo", method = RequestMethod.GET)
    public Object getAftersalesInfo(Long id) {
        CommonResult commonResult = new CommonResult();

        Long memberId = memberService.getNewCurrentMember().getId();

        if (memberId == null) {
            return commonResult.failed("请登录");
        }

        if (id == null) {
            return commonResult.paramFailed();
        }

        AftersalesDetailsVo aftersalesInfo = iOmsAftersalesService.getAftersalesInfo(id);

        return commonResult.success(aftersalesInfo);
    }

    @ApiOperation("申请售后详情")
    @RequestMapping(value = "/applyAftersalesInfo", method = RequestMethod.GET)
    public Object ApplyAftersalesInfo(Long id) {
        CommonResult commonResult = new CommonResult();

        Long memberId = memberService.getNewCurrentMember().getId();

        if (memberId == null) {
            return commonResult.failed("请登录");
        }

        if (id == null) {
            return commonResult.paramFailed();
        }

        ApplyAftersalesInfoVo applyAftersalesInfoVo = iOmsAftersalesService.ApplyAftersalesInfo(id);

        return commonResult.success(applyAftersalesInfoVo);
    }

    @ApiOperation("提交售后申请")
    @RequestMapping(value = "/commiterAftersales", method = RequestMethod.POST)
    public Object commiterAftersales(ApplyAftersalesDto applyAftersalesDto) {
        CommonResult commonResult = new CommonResult();

        Long memberId = memberService.getNewCurrentMember().getId();

        if (memberId == null) {
            return commonResult.failedIsNull("请登录");
        }

        iOmsAftersalesService.commiterAftersales(memberId, applyAftersalesDto);
        return commonResult.success();

    }


}
